//
//  AppDelegate.swift
//  XmppTry
//
//  Created by Arthur on 15/12/16.
//  Copyright © 2015年 Arthur. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,XMPPStreamDelegate{
  
    var window: UIWindow?

    //通道定义
    var xs:XMPPStream?
    
    //服务器是否开启
    var isOpen = false
    
    //密码
    var pwd = ""
    
    
    //状态state代理
    var statedelegate: StateDelegate?
    //消息代理
    var messagedelegate:MessageDelegate?
    
    
    //收到消息
    
    func xmppStream(sender: XMPPStream!, didReceiveMessage message: XMPPMessage!) {
        
      
        
        if message.isChatMessage(){
            
            
            var msg = WXMessage()
            
            
            
            
            //对方正在输入(其中的composing是官方文档写的，就如同下面的available）
            if message.elementForName("composing") != nil{
                msg.isComposing = true
            }
            
            
            //离线消息
            if message.elementForName("delay") != nil{
                msg.isDelayL = true
            }
            
            //正文
            if let body = message.elementForName("body"){
                msg.body = body.stringValue()
            }
            
            //完整用户名
            msg.from = message.from().user + "@" + message.from().domain
            
            //添加到消息代理中
            messagedelegate?.newMsg(msg)
            
        }

        
        
        
        
    }
    
    
    
   
    
    
    //收到状态
    func xmppStream(sender: XMPPStream!, didReceivePresence presence: XMPPPresence!) {
        //我自己的用户名
        let myUser = sender.myJID.user
        //好友的账户名
        let user = presence.from().user
        
        //用户所在的域
        let domain = presence.from().domain
        
        //状态类型
        let pType = presence.type()
        
        
        //不是自己的状态
        if(user != myUser){
            //状态保存的结构
            var state = State()
            //保存状态的用户名(完整）
            state.name = user + "@" + domain
            
            
            //上线
            if (pType == "available"){
                state.isOnline = true
                statedelegate?.isOn(state)
            }
            else if pType == "unavailable"{
                //默认状态false，不变
                statedelegate?.isOff(state)
            }
            
            
        }
    }
    
    
    
    
    //连接成功
    func xmppStreamDidConnect(sender: XMPPStream!) {
        isOpen = true
        //验证密码
        try?xs!.authenticateWithPassword(pwd)
        
        
    }
    
    //验证成功
    func xmppStreamDidAuthenticate(sender: XMPPStream!) {
        
        goOnline()
    }
    

    
    
    //建立通道
    func buildStream(){
        xs = XMPPStream()
        xs?.addDelegate(self, delegateQueue: dispatch_get_main_queue())
        
    }
    
    
    
    //发送上线状态
    func goOnline(){
        let p = XMPPPresence()
        
        xs!.sendElement(p)
    
    }
    
    
    //发送下限状态
    func goOffline(){
        let p = XMPPPresence(type:"unavailable")
        xs!.sendElement(p)
        
    }
    
    
    //链接服务器（查看服务器是否可连接)
    func connect() ->Bool {
        buildStream()
        
        //通道已经连接
        if xs!.isConnected(){
            return true
        }
        let user = NSUserDefaults.standardUserDefaults().stringForKey("weixinID")
        let password = NSUserDefaults.standardUserDefaults().stringForKey("weixinPwd")
        let server = NSUserDefaults.standardUserDefaults().stringForKey("wxserver")
        
        if (user != nil && password != nil){
            
            xs!.myJID = XMPPJID.jidWithString(user!)
            xs!.hostName = server!
            
            pwd = password!
            
            
            
            do{
               try xs!.connectWithTimeout(5000)
            }
            catch{
                
            }
 
        }
        
        
        return false
    }
    
    // 
    func disConnect(){
        if xs != nil  {
            if xs!.isConnected(){
                
                goOffline()
                xs!.disconnect()
            }
   
        }
     
    }
    
    
    
    
    
    
    
    
    
    
    
    

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

